package br.agenda.crud.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.spi.DirStateFactory.Result;

import br.agenda.crud.db.ConexaoMySQL;
import br.com.agenda.crud.Contato;

public class AgendaDAO {
	public static Statement stmt = ConexaoMySQL.conectar();
	public static void persistContato(Contato c) throws SQLException {
		
		if(c.getId() == 0){
			stmt.execute("Insert INTO agenda (nome,telefone) VALUES ('"+c.getNome()+"',"+c.getTelefone()+");"); 
		}else{
			stmt.execute("UPDATE agenda SET nome = '"+c.getNome()+"' , telefone = "+c.getTelefone()+"  where id = "+c.getId()+"");	
		}
	}
	public static ResultSet selectContatos() throws SQLException{
		ResultSet rs = stmt.executeQuery("select * from agenda");
		return rs;
	}
	public static void deleteContato(Contato contato) throws SQLException{
		stmt.execute("DELETE FROM agenda where id = "+contato.getId());		
	}

}
